<template>
  <div class="login-container">
    <el-card class="login-main">
      <div class="title"><span>vite-drag</span></div>
      <el-form class="login-main-form" ref="loginFormRef" :model="loginForm" label-placement="left" label-align="right"
        :label-width="0">
        <el-form-item prop="username">
          <el-input v-model.trim="loginForm.username" @keyup.enter.native="handleLogin">
            <template #prefix>
              <i class="iconfont icon-yonghu"></i>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model.trim="loginForm.password" show-password @keyup.enter.native="handleLogin">
            <template #prefix>
              <i class="iconfont icon-mima"></i>
            </template>
          </el-input>
        </el-form-item>
        <el-form-item prop="captcha" class="captcha-form">
          <el-input v-model.trim="loginForm.captcha" type="text" @keyup.enter.native="handleLogin">
          </el-input>
          <div v-html="captcha" class="captcha" @click="getCaptcha"></div>
        </el-form-item>
        <el-form-item>
          <el-button class="login-main-form-btn" type="primary" @click="handleLogin" v-loading="loginLoading">登录
          </el-button>
        </el-form-item>
      </el-form>
    </el-card>
  </div>
</template>

<script>
import { defineComponent, onMounted, ref } from "vue";
import { useRouter } from "vue-router";
import { getCaptchaAPI } from "../../service/user.js";
import { useUserStore } from "../../store/user";

export default defineComponent({
  name: "Login",
  setup() {
    const loginForm = ref({
      username: "admin",
      password: "admin",
      captcha: "",
    });
    const loginLoading = ref(false);
    const captcha = ref();
    const rules = ref({
      username: {
        required: true,
        message: "请输入用户名",
        trigger: "blur",
      },
      password: {
        required: true,
        message: "请输入密码",
        trigger: ["input", "blur"],
      },
    });
    const loginFormRef = ref();
    const user = useUserStore();
    const router = useRouter();
    const handleLogin = () => {
      loginLoading.value = true;
      loginFormRef.value.validate((valid) => {
        if (valid) {
          user
            .loginByUsername(loginForm.value)
            .then((res) => {
              loginLoading.value = false;
              router.push("/project/list");
            })
            .catch((err) => {
              loginLoading.value = false;
              getCaptcha();
            });
        } else {
          loginLoading.value = false;
          getCaptcha();
        }
      });
    };
    const getCaptcha = () => {
      getCaptchaAPI().then((res) => {
        captcha.value = res.data.data.result;
      });
    };
    onMounted(() => {
      getCaptcha();
    });
    return {
      user,
      loginLoading,
      loginForm,
      rules,
      loginFormRef,
      captcha,
      handleLogin,
      getCaptcha,
    };
  },
});
</script>

<style scoped lang="less">
.login-container {
  height: 100vh;
  background: url("https://fengnull.gitee.io/picture_house/bg1.jpg") no-repeat;
  background-size: 100% 100%;

  .login-main {
    background: rgba(0, 0, 0, 0.5);
    width: 560px;
    height: 380px;
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    margin: auto;
    box-shadow: -15px 15px 15px rgba(6, 17, 47, 0.7);
    z-index: 2004;
    border: 0;

    .title {
      user-select: none;
      padding: 30px 0 25px;
      text-align: center;
      color: #ffffff;
      font-size: 32px;
      font-weight: bold;
    }

    .login-main-form {
      padding: 0 36px;

      .login-main-form-btn {
        width: 100%;
        border: 1px solid #4fa1d9;
      }

      .captcha-form {
        :deep(.el-form-item__content) {
          display: flex;
          align-items: center;
          .el-input {
            flex: 1;
          }
        }
      }

      .captcha {
        padding: 0 12px;
        flex: 0 0 65px;
        cursor: pointer;
        svg {
          display: block;
          margin: auto;
          height: 30px;
        }
      }
    }
  }
}
</style>
